﻿using System.Net;
using System.Xml.Serialization;

namespace Toybox.NicoSharp.Live
{
	using Extensions;
	using Primitives;
	using Utilities;

	[XmlRoot("getplayerstatus")]
	public partial class PlayerStatus : AbstractLiveStatus
	{

		#region [Static]

		public static PlayerStatus Get(string completeId, CookieCollection cookies = null)
		{
			string url = NicoUrls.WatchLiveApi + "getplayerstatus/" + completeId;
			string xml = NicoWebClient.Get(url, cookies.Coerce());
			PlayerStatus ps = Deserializer.XmlDeserializer<PlayerStatus>(xml); ;
			ps.Cookies = cookies;

			return ps;
		}

		private static void DerParse(byte[] key)
		{
			int position = 0;

			// type
			int type = key[position++];
			bool constructed = (type & 0x20) != 0;
			type &= 0x1F;

			// length
			int length = key[position++];
			if (length >= 0x80)
			{
				int count = length & 0x7F;
				length = 0;

				for (; count > 0; count--)
					length = (length << 8) | key[position++];
			}

			// data
			switch (type)
			{
				case 0x00:
				case 0x10:



					break;
				case 0x11:
					break;
				case 0x02:
					break;
				case 0x06:
					break;
				default:
					break;
				case 0x03:
					break;
				case 0x04:
					break;
				case 0x05:
					break;
				case 0x13:
					break;
				case 0x22:
				case 0x14:
					break;
				case 0x17:
					break;
			}
		}

		#endregion [Static]


		#region Public Members

		#region PlayerStatus Members

		/// <summary>
		/// ストリーム情報
		/// </summary>
		public Stream stream;

		/// <summary>
		/// ユーザ情報
		/// </summary>
		public User user;

		/// <summary>
		/// RTMP情報
		/// </summary>
		public RTMP rtmp;

		/// <summary>
		/// メッセージサーバ情報
		/// </summary>
		public MS ms;

		/// <summary>
		/// tidのリスト
		/// </summary>
		[XmlArrayItem("tid")]
		public Tid[] tid_list;

		/// <summary>
		/// チケットリスト
		/// </summary>
		[XmlArrayItem("stream")]
		public TStream[] tickets;

		/// <summary>
		/// ツイッター情報
		/// </summary>
		public Twitter twitter;

		/// <summary>
		/// プレーヤー情報
		/// </summary>
		public Player player;

		/// <summary>
		/// マーキー
		/// </summary>
		public Marquee marquee;

		#endregion PlayerStatus Members

		[XmlIgnore]
		public CookieCollection Cookies
		{
			get;
			protected set;
		}

		#endregion Public Members

	}
}